<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('storeid'=>$this->store_id,'merchid'=>0);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','groupid','status','workerid','isblack','createtime','level');
	$list=pdo_getslice('jy_weishop_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$member=model('core/member')->detail($item['mid'],array('credit1','credit2'));
			$item=array_merge($member,$item);
			$item['order_num']=$this->count('order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'status'=>3));
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'member','title'=>'基本信息','type'=>'member'),
			array('name'=>'level','title'=>'等级','type'=>'select','options'=>'levels'),
			array('name'=>'groupid','title'=>'分组','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'绑定的店员','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'credit1','title'=>'积分','type'=>'string'),
			array('name'=>'credit2','title'=>'余额','type'=>'string'),
			array('name'=>'order_num','title'=>'已成交订单数','type'=>'string'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
			array('name'=>'isblack','title'=>'加入黑名单','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'modal','action'=>'core/member.member/recharge','title'=>'充值'),
			array('type'=>'modal','action'=>'core/member.member/cpassword','title'=>'重置密码'),
		)),
		'search_one'=>array('name'=>'mid','title'=>'会员id','add'=>false),
		
	);
	$return['levels']=$this->getall('member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=$this->getall('member_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('store_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['order_count']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'status'=>3));
		$sql = "SELECT SUM(price) FROM " . tablename('jy_weishop_order') . " WHERE uniacid=:uniacid AND mid=:mid AND status=3";
		$money = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":mid" => $item['mid']));
		$item['order_money']=$money['SUM(price)'];
		$member=model('core/member')->detail($item['mid'],array('nickname','avatar','mobile','realname'));
		$item['nickname']=$member['nickname'];
		$item['avatar']=$member['avatar'];
		$item['mobile']=$member['mobile'];
		$item['realname']=$member['realname'];
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回会员列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'会员昵称','name'=>'nickname','help'=>''),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'select','title'=>'会员组','name'=>'groupid','options'=>'group','help'=>''),
					array('type'=>'select','title'=>'会员等级','name'=>'level','options'=>'levels','help'=>''),
					array('type'=>'string','title'=>'真实姓名','name'=>'realname','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'注册时间','name'=>'createtime','help'=>'','read'=>true),
					array('type'=>'switch','title'=>'加入黑名单','name'=>'isblack','help'=>'设置黑名单后，此会员无法访问商城'),
					
				)
			),
				array(
				'title'=>'交易信息',
				'list'=>array(
					array('type'=>'string','title'=>'成交订单数','name'=>'order_count','help'=>'','read'=>true),
					array('type'=>'string','title'=>'成交额','name'=>'order_money','help'=>'','read'=>true),
				)
			),
		)
	);
	
	$return['group']=pdo_getall('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['levels']=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'groupid'=>intval($postdata['groupid']),
		'level'=>intval($postdata['level']),
		'isblack'=>intval($postdata['isblack']),
	);
	$member=array(
		'nickname'=>trim($postdata['nickname']),
		'avatar'=>trim($postdata['avatar']),
		'realname'=>trim($postdata['realname']),
	);
	$id=intval($postdata['id']);
	$mid=intval($postdata['mid']);
	if($id){
		pdo_update('jy_weishop_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('core_members',$member,array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_weishop_member',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}